package com.yazi.mxz.person.controller.shop;

import javax.annotation.Resource;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.service.StoreService;
import com.yazi.mxz.person.jackson.filter.StoreFilter;

@Controller("shop-storeController")
@RequestMapping("/shop/store")
public class StoreController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreController.class);
	
	@Resource
	StoreService storeService;
	
	@RequestMapping("/edit")
	public String edit(Model model) {
		debug(log, "进入店铺信息编辑页面");
		model.addAttribute("store", getStore(true));
		return "shop/store_edit";
	}
	
	@RequestMapping("/editDo/json")
	@ResponseBody
	public StatusResult editDoJson(Store store) {
		debug(log, "保存店铺信息");
		StatusResult jsonResult = new StatusResult();
		try {
			// 防止篡改以下信息
			store.setCompany(null);
			store.setIsNewOrder(null);
			store.setStoreId(null);

			storeService.editByStore(store, getStore(true));
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "店铺信息保存异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/onlineSetting")
	public String onlineSetting() {
		debug(log, "进入线上销售设置页面");
		return "shop/store_onlineSetting";
	}
	
	@RequestMapping("/localSetting")
	public String localSetting() {
		debug(log, "进入线下销售设置页面");
		return "shop/store_localSetting";
	}
	
	@RequestMapping("/localSettingDo/json")
	@ResponseBody
	public StatusResult localSettingDoJson(String localSetting) {
		debug(log, "保存线下销售设置");
		StatusResult jsonResult = new StatusResult();
		try {
			Store store = getStore(true);
			store.setLocalSetting(localSetting);
			storeService.merge(store);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "线下销售设置保存异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/get/json")
	@ResponseBody
	public ObjectResult<?> getJson() {
		debug(log, "获取店铺信息");
		ObjectResult<Store> jsonResult = new ObjectResult<Store>();
		jsonResult.setFilter(StoreFilter.get());
		try {
			jsonResult.setResult(getStore(true));
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取店铺信息异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
}
